<?php
// -----------------------------------------------------------------------------
include "global.php";
include "auth.php";
// -----------------------------------------------------------------------------

// 设置时区
date_default_timezone_set($region);

$today = new DateTime("today");
$todayStr = $today->format("Ymd");

$tomorrow = $today->add(new DateInterval("P1D")); // $today 的值这里会改变
$tomorrowStr = $tomorrow->format("Ymd");

// -----------------------------------------------------------------------------
?>
<html>
<head>
    <meta charset="utf-8">
    <title>Todo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="resources/bootstrap-5.2.0-beta1-dist/css/bootstrap.min.css">
</head>
<body>

<!-- ----------------------------------------------------------------------- -->
<?php include "globalNavbar.php"; ?>
<!-- ----------------------------------------------------------------------- -->

<div class="container container-fluid mt-3">

    <div class="list-group">
        <a href="viewByDate.php?date=<?php echo $todayStr; ?>" class="list-group-item list-group-item-action">查看「日程 of 今天」</a>
        <a href="viewByDate.php?date=<?php echo $tomorrowStr; ?>" class="list-group-item list-group-item-action">查看「日程 of 明天」</a>
        <a href="#" class="list-group-item list-group-item-action" data-bs-toggle="modal" data-bs-target="#specificDate">查看「日程 of 指定日期」</a>
        <a href="#" class="list-group-item list-group-item-action" data-bs-toggle="modal" data-bs-target="#modalRepeat" >重复事件 - 指定</a>
        <a href="#" class="list-group-item list-group-item-action" data-bs-toggle="modal" data-bs-target="#listOfDatesWithTask">重复事件 - 查看</a>
        <a href="view.php?from=goals" class="list-group-item list-group-item-action">文档 - 目标与行动计划</a>
        <a href="view.php?from=storageBox" class="list-group-item list-group-item-action">文档 - 待办事务收纳箱</a>
    </div>

    <div class="list-group mt-3">
        <a id="recreateDB" href="#" class="list-group-item list-group-item-action" data-bs-toggle="modal" data-bs-target="#modalConfirmRecreateDB">重置数据库</a>
        <a href="about.php" class="list-group-item list-group-item-action">关于</a>
    </div>

</div>

<!----------------------------------------------------------------------------->

<!-- Modal -->
<!--查看指定日期-->
<div class="modal fade" tabindex="-1" id="specificDate" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="true">
    <div class="modal-dialog modal-dialog-scrollable">
        <div class="modal-content">

            <div class="modal-header">
                <h1 class="modal-title fs-5" id="exampleModalLabel">查看日程 of 指定日期</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>

            <div class="modal-body">

                <div class="input-group mb-3">
                    <input id="inputDate" name="date" type="date" class="form-control">
                    <button class="btn btn-outline-secondary" type="button" id="btnViewSpecificDate">查看</button>
                </div>

            </div>

        </div>
    </div>
</div>


<!-- Modal -->
<!--编辑重复-->
<div class="modal fade" tabindex="-1" id="modalRepeat" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="true">
    <div class="modal-dialog modal-dialog-scrollable">
        <div class="modal-content">

            <div class="modal-header">
                <h1 class="modal-title fs-5" id="exampleModalLabel">重复事件 - 指定</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>

            <div class="modal-body">

                <div class="mt-2">
                    <h3>每日重复</h3>
                    <div class="list-group">
                        <a href="edit.php?from=daily" class="list-group-item list-group-item-action">编辑</a>
                    </div>
                </div>

                <div class="mt-2">
                    <h3>每周重复</h3>
                    <div class="list-group">
                        <a href="edit.php?from=weekly&dayOfWeek=1" class="list-group-item list-group-item-action">周一</a>
                        <a href="edit.php?from=weekly&dayOfWeek=2" class="list-group-item list-group-item-action">周二</a>
                        <a href="edit.php?from=weekly&dayOfWeek=3" class="list-group-item list-group-item-action">周三</a>
                        <a href="edit.php?from=weekly&dayOfWeek=4" class="list-group-item list-group-item-action">周四</a>
                        <a href="edit.php?from=weekly&dayOfWeek=5" class="list-group-item list-group-item-action">周五</a>
                        <a href="edit.php?from=weekly&dayOfWeek=6" class="list-group-item list-group-item-action">周六</a>
                        <a href="edit.php?from=weekly&dayOfWeek=0" class="list-group-item list-group-item-action">周日</a>
                    </div>
                </div>

                <div class="mt-2">

                    <h3>每月重复</h3>
                    <div class="list-group">
                        <?php
                        $counter = 1;
                        while ($counter < 32) {

                            if ($counter < 10) {
                                $counter = "0" . $counter;
                            }

                            echo <<<BBB
                            <a href="edit.php?from=monthly&dayOfMonth={$counter}" class="list-group-item list-group-item-action">{$counter} 号</a>
                            BBB;

                            echo PHP_EOL;
                            $counter += 1;
                        }
                        ?>
                    </div>

                </div>

                <div class="mt-2">
                    <h3>每年重复</h3>
                    <div class="mt-2">
                        <ul>
                            <li>月份与天数不足两位数时，用 0 补足。如「1月7号」写作「0107」，</li>
                            <li>不使用分隔符号，如「-」或「/」</li>
                        </ul>
                    </div>
                    <div class="input-group mb-3">
                        <input id="inputMonthAndDay" name="monthAndDay" type="number" class="form-control">
                        <button id="btnEditYearly" class="btn btn-outline-secondary" type="button">编辑</button>
                    </div>
                </div>

            </div>
<!---->
<!--            <div class="modal-footer">-->
<!--                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>-->
<!--            </div>-->

        </div>
    </div>
</div>


<!-- Modal -->
<!--确认重置数据库-->
<div class="modal fade" tabindex="-1" id="modalConfirmRecreateDB" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="true">
    <div class="modal-dialog modal-dialog-scrollable">
        <div class="modal-content">

            <div class="modal-header">
                <h1 class="modal-title fs-5" id="exampleModalLabel">确认重置数据库？</h1>
                <button id="btnCloseModalConfirmRecreateDB" type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>

            <div class="modal-body">
                <p>请在下方输入框中输入「sure」以确认删除。</p>
                <p>该操作<span style="color: red">无法撤消</span>，请谨慎选择。</p>
                <input id="inputSure" type="text" class="form-control">

            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button id="btnSureToRecreateDB" type="button" class="btn btn-primary">确认</button>
            </div>

        </div>
    </div>
</div>


<!-- Modal -->
<!-- List of Dates with Task -->
<div class="modal fade" tabindex="-1" id="listOfDatesWithTask" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="true">
    <div class="modal-dialog modal-dialog-scrollable">
        <div class="modal-content">

            <div class="modal-header">
                <h1 class="modal-title fs-5" id="exampleModalLabel">重复事件 - 查看</h1>
                <button id="btnCloseModalListOfDatesWithTask" type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>


            <div class="modal-body">

                <div class="mt-2">
                    <h3>每日重复</h3>
                    <div class="list-group">

                        <a href="edit.php?from=daily" class="list-group-item list-group-item-action">每日</a>
                    </div>
                </div>


                <?php
                    $result = $db->getFutureDatesWithTask($todayStr);
                    $dayOfWeeks = $result["dayOfWeek"];
                    $dayOfMonths = $result["dayOfMonth"];
                    $monthAndDays = $result["monthAndDay"];
                    $specificDates = $result["specificDate"];
                ?>

                <?php if (count($dayOfWeeks) !== 0): ?>

                    <div class="mt-2">
                        <h3>每周重复</h3>
                        <div class="list-group">
                            <?php
                            $days = ["日", "一", "二", "三", "四", "五", "六"];
                            foreach ($dayOfWeeks as $dayOfWeek) {
                                $week = $days[$dayOfWeek];
                                echo <<<BBB
                            <a href="edit.php?from=weekly&dayOfWeek={$dayOfWeek}" class="list-group-item list-group-item-action">每周{$week}</a>
                            BBB;
                            }
                            ?>
                        </div>
                    </div>

                <?php endif; ?>

                <?php if (count($dayOfMonths) !== 0): ?>

                    <div class="mt-2">
                        <h3>每月重复</h3>
                        <div class="list-group">
                            <?php
                            foreach ($dayOfMonths as $dayOfMonth) {

                                if ($dayOfMonth < 10) {
                                    $dayOfMonth = "0" . $dayOfMonth;
                                }

                                echo <<<BBB
                            <a href="edit.php?from=monthly&dayOfMonth={$dayOfMonth}" class="list-group-item list-group-item-action">每月 {$dayOfMonth} 号</a>
                            BBB;
                            }
                            ?>
                        </div>
                    </div>

                <?php endif; ?>

                <?php if (count($monthAndDays) !== 0): ?>

                    <div class="mt-2">
                        <h3>每年重复</h3>
                        <div class="list-group">
                            <?php
                            foreach ($monthAndDays as $monthAndDay) {
                                echo <<<BBB
                            <a href="edit.php?from=yearly&monthAndDay={$monthAndDay}" class="list-group-item list-group-item-action">每年 {$monthAndDay}</a>
                            BBB;
                            }
                            ?>
                        </div>
                    </div>

                <?php endif; ?>

                <?php if (count($specificDates) !== 0): ?>

                    <div class="mt-2">
                        <h3>指定日期</h3>
                        <div class="list-group">
                            <?php
                            foreach ($specificDates as $date) {
                                echo <<<BBB
                    <a href="edit.php?from=specificDate&date={$date}" class="list-group-item list-group-item-action">{$date}</a>
                    BBB;
                            }
                            ?>
                        </div>
                    </div>

                <?php endif; ?>

            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">确认</button>
            </div>

        </div>
    </div>
</div>


<!----------------------------------------------------------------------------->

<script src="resources/jQuery/jquery-3.5.1.js"></script>
<script src="resources/bootstrap-5.2.0-beta1-dist/js/bootstrap.min.js"></script>

<!----------------------------------------------------------------------------->

<script>
    $("#btnViewSpecificDate").click(function () {
        var inputedDate = $("#inputDate").val()
        if (inputedDate !== "") {
            var arr = inputedDate.split("-")
            inputedDate = arr[0] + arr[1] + arr[2]
            var url = "viewByDate.php?date=" + inputedDate
            window.open(url, "_self")
        } else {
            alert("请先选择日期。")
        }
    })
    $("#btnEditWeekly").click(function () {
        var dayOfWeek = $("#inputDayOfWeek").val()
        var url = "edit.php?from=weekly&dayOfWeek=" + dayOfWeek
        window.open(url, "_self")
    })
    $("#btnEditMonthly").click(function () {
        var dayOfMonth = $("#inputDayOfMonth").val()
        var url = "edit.php?from=monthly&dayOfMonth=" + dayOfMonth
        window.open(url, "_self")
    })
    $("#btnEditYearly").click(function () {
        var monthAndDay = $("#inputMonthAndDay").val()
        var url = "edit.php?from=yearly&monthAndDay=" + monthAndDay
        window.open(url, "_self")
    })
</script>

<!----------------------------------------------------------------------------->

<script>
    $("#btnSureToRecreateDB").click(function () {

        var inputtedContent = $("#inputSure").val()
        $("#inputSure").val("")

        if (inputtedContent == "sure") {

            $("#btnCloseModalConfirmRecreateDB").click()
            var url = "recreateDB.php"

            $.get(url)
                .done(function( data ) {
                    alert(data)
                })
                .fail(function () {
                    alert("请求失败。")
                });

        }

    })
</script>

<!----------------------------------------------------------------------------->

</body>
</html>
